#include "Socket.hpp"
#include "protocol.hpp"
#include <time.h>
#include <cassert>
void Usage(const std::string &proc)
{
    std::cout << "/nUsage " << proc << " port[1024+]" << std::endl;
}
int main(int argc, char *argv[])
{
    if (argc != 3)
    {
        Usage(argv[0]);
        exit(1);
    }
    std::string serverip = argv[1];
    uint16_t serverport = std::stoi(argv[2]);
    Sock sockfd;
    sockfd.Socket();
    bool r = sockfd.Connect(serverip, serverport);
    if (!r)
        return 1;
    //
    int cnt = 1;
    srand(time(nullptr));
    const std::string opers = "+-*/%=$";
    std::string inbuff_stream;
    while (cnt <= 10)
    {
        std::cout << "==============" << "第" << cnt << "次测试" << "=========" << std::endl;
        int x = rand() % 100 + 1;
        int y = rand() % 100;
        char op = opers[rand() % opers.size()];
        Request req(x, y, op);
        // req.DebugPrint();
        std::string content;
        req.Serialize(&content);
        content = Enable(content);
        write(sockfd.Fd(), content.c_str(), content.size());
        char buff[128];
        ssize_t n = read(sockfd.Fd(), buff, sizeof(buff));
        std::cout << n << std::endl;
        if (n > 0)
        {
            buff[n] = 0;
            std::cout << buff;
            inbuff_stream += buff;
            // std::cout << inbuff_stream;
        }
        Response resp;
        content = "";
        Deable(inbuff_stream, &content);
        std::cout << content << std::endl;
        // assert(r);
        resp.Deserialize(content);
        // assert(r);
        resp.DebugPrint();
        cnt++;
        sleep(1);
        std::cout << "==============" << std::endl;
    }

    return 0;
}